﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities.Presentation;
using System.Activities.Core.Presentation;
using System.Activities.Statements;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Activities.Presentation.View;
using System.ComponentModel;

namespace WF4Host
{
    public class HostingHelper
    {
        public WorkflowDesigner Designer { get { return workflowDesigner;} }
        WorkflowDesigner workflowDesigner;



        public static HostingHelper GetDesignerCommandTarget(DependencyObject obj)
        {
            return (HostingHelper)obj.GetValue(DesignerCommandTargetProperty);
        }

        public static void SetDesignerCommandTarget(DependencyObject obj, HostingHelper value)
        {
            obj.SetValue(DesignerCommandTargetProperty, value);
        }

        // Using a DependencyProperty as the backing store for DesignerCommandTarget.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DesignerCommandTargetProperty =
            DependencyProperty.RegisterAttached("DesignerCommandTarget", typeof(HostingHelper), typeof(HostingHelper), new UIPropertyMetadata(null,new PropertyChangedCallback(OnCommandTargetChanged)));

        protected static void OnCommandTargetChanged(DependencyObject depo, DependencyPropertyChangedEventArgs depa)
        {
            var src = depo as ICommandSource;
            if (null != src && depa.NewValue != null )
            {
                var ctargetdescr = DependencyPropertyDescriptor.FromName("CommandTarget", depo.GetType(), depo.GetType(),true);
                ctargetdescr.SetValue(depo, (depa.NewValue as HostingHelper).Designer.Context.Services.GetService<DesignerView>());
            }
        }


        public HostingHelper()
        {
            workflowDesigner = new WorkflowDesigner();
            new DesignerMetadata().Register();
            //Creates an empty workflow
            workflowDesigner.Load(new Sequence());
            
        }
        
        
    }
    
}
